<?php
namespace app\mobile\controller;

use app\model\Theme; 

class Cbw extends Base
{
    public function Cbw()
    {
/**
 * Author: cbw
 * Desc:
 * Time: 2023/8/9 08:22
 * Email: 17854288794@163.com
 */
global $_P,$_G;
$system=$_G['system'];
$config =   Theme::getConfig(1);
$op = isset($_P['op'])?$_P['op']:'display';
$themeid = isset($_P['themeid'])?$_P['themeid']:'1';
$theme = Theme::getThemeById($themeid);
$module_name = 'xe17818';
$is_all = isset($_P['is_all'])?$_P['is_all']:0;
//判断17服务器 5服务器
if ((DOMAIN0=='17.yixuan.net' || DOMAIN0=='5.yixuan.net') && MODULE_NAME==$module_name){
    AutoShipment::updateRecordFiled();
    //todo  查询已支付订单
    $goods_desc = $theme['title'];
    $sql = " select a.*,b.order_shipment_state,b.id as record_id from ".tablename(t_order)." as a left join ".tablename(t_record)." as b on a.themeid=b.themeid and a.detailid=b.id".
        " where a.themeid='".$theme['id']."' and a.paystate=1 and b.order_shipment_state=1";
    if (!$is_all){
        $item = db_find($sql);
        $order_list[] =  $item;
    }else{
        $order_list = db_select($sql);
    }
    foreach ($order_list as $k => $val){
        //存在微信支付单号  存在openid
        if ($val['transaction_id'] && $val['openid']){
            $transaction_id = $val['transaction_id'];
            //查询微信支付订单发货状态
            $get_order = AutoShipment::getOrder($system,$transaction_id);
            if ($get_order){
                if ($get_order['errcode']==48001){
                    //不存在接口 修改config中参数
                    yxlog('失败:当前订单或小程序不支持自动发货','小程序自动发货');
                    echo "失败:定单号:".($val['record_id']+9000).";当前订单或小程序不支持自动发货<br/>";
                }elseif ($get_order['errcode']==0){
                    if ($val['order_shipment_state']==1){
                        //存在接口
                        $autoship = AutoShipment::sureShipment($system,$transaction_id,$goods_desc,$val['openid']);
                        if ($autoship['errcode']==0){
                            //更新arecord 中订单发货状态
                            $update = ['order_shipment_state'=>2];
                            $where = ['themeid'=>$theme['id'],'id'=>$val['detailid']];
                            db_update(t_record,$update,$where);
                            yxlog('成功:定单号:'.($val['record_id']+9000).';自动发货成功','小程序自动发货');
                            echo "成功:定单号:".($val['record_id']+9000).";自动发货成功<br/>";
                        }else{
                            $tmp_msg = AutoShipment::getErrorInfo($autoship['errcode'],$autoship['errmsg']);
                            yxlog('失败:定单号:'.($val['record_id']+9000).';'.$tmp_msg,'小程序自动发货');
                            echo "失败:定单号:".($val['record_id']+9000).";".$tmp_msg."<br/>";
                        }
                    }else{
                        if ($val['order_shipment_state']!=2){
                            yxlog('异常:定单号:'.($val['record_id']+9000).';状态无法自动发货','小程序自动发货');
                            echo "异常:定单号:".($val['record_id']+9000).";状态无法自动发货<br/>";
                        }
                    }
                }
            }
        }
    }
}else{
    yxlog('失败:当前后台不支持小程序自动发货功能','小程序自动发货');
    echo "失败:当前后台不支持小程序自动发货功能<br/>";
}

}
}